<template>
  <div class="image-loader-container">
    <img :src="placeholder" alt="" v-if="!everything" class="placeholder-img">
    <img :src="src" alt="" @load="load" class="origin-img" :style="{opacity: originOpacticy, transition: `${duration}ms`}">
  </div>
</template>

<script>
export default {
  data() {
    return {
      originLoaded: false,
      everything: false
    }
  },
  props: {
    src: {
      required: true,
      type: String,
      default: () => ''
    },
    placeholder: {
      required: true,
      type: String,
      default: () => ''
    },
    duration: {
      required: false,
      type: Number,
      default: () => 500
    }
  },
  computed: {
    originOpacticy() {
      return this.originLoaded ? 1 : 0
    }
  },
  methods: {
    load() {
      this.originLoaded = true

      setTimeout(() => {
        this.everything = true
        this.$emit('load')
      }, this.duration)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/common.scss';
.image-loader-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  img {
    @include self-fill();
    object-fit: cover;
  }
  .placeholder-img {
    filter: blur(2vw);
  }
}
</style>